package com.common.utils
{
	import com.common.formatters.NumberBaseRoundType;
	import com.common.formatters.NumberFormatter;
	
	import flash.geom.Point;
	/**
	 *格式化数字 
	 * @author lake
	 * 
	 */	
	public class NumberUtil
	{
		public function NumberUtil()
		{
		}
		/**
		 *格式化數據 
		 * @param num 數據大小
		 * @param precision 小數點后位數
		 * @return 
		 */		
		public static function formatNumber(num:Number,precision:int = 0):String
		{
			var numFormat :NumberFormatter = new NumberFormatter();
			numFormat.precision = precision;
			numFormat.useThousandsSeparator = true;
			numFormat.rounding = NumberBaseRoundType.NEAREST;
			
			return numFormat.format(num);
		}
		/**
		 * 把有格式的字符串转化为Number
		 * @param value
		 * @param thousandsSeparator
		 * @return 
		 * 
		 */		
		public static function formatStringToNumber(value:String,thousandsSeparator:String = ",",precision:int = 2):Number
		{
			if(value == null)
			{
				return 0;
			}
			var myPattern:RegExp = /,/g;
			
			var destStr :String = value.replace(myPattern,"");
			var destNum :Number = Number(destStr);
			
			var numFormat :NumberFormatter = new NumberFormatter();
			numFormat.precision = precision;
			numFormat.rounding = NumberBaseRoundType.NEAREST;
			
			var formatSource :String = numFormat.format(destNum);
			
			var formatStr :String = formatSource.replace(myPattern,"");
			
			return Number(formatStr);
		}
		/**
		 *是否是奇数 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function isOdd(value:int):Boolean
		{
			if(value%2 == 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		/**
		 * 获取两点之间的角度 x 轴基线
		 * y 轴 正常 上大 下小
		 * @param p1
		 * @param p2
		 * @return  degrees
		 * 
		 */		
		public static function getTwoPointAngle(p1 :Point,p2 :Point):Number
		{
			var absX :Number = p2.x - p1.x;
			var absY :Number = p2.y - p1.y;
			var tanVal :Number = Math.abs(absY / absX);
			var degrees :Number = Math.atan(tanVal) * 180 / Math.PI;
			
			if(degrees == 0 && absX < 0 )//x 轴反
			{
				degrees = 180;
			}
			if(degrees == -90)//y 轴反
			{
				degrees = 270;
			}
			if(absX < 0 && absY > 0)//第二象限
			{
				degrees = 180 - degrees;
			}
			if(absX < 0 && absY < 0)//第三象限
			{
				degrees += 180;
			}
			if(absX > 0 && absY < 0)//第四象限
			{
				degrees = 360 - degrees;
			}
			return degrees;
		}
		/**
		 * 获取两点之间的角度 x 轴基线
		 * y 轴 相反 上小 下大
		 * @param p1
		 * @param p2
		 * @return  degrees
		 * 
		 */		
		public static function getTwoPointAngleRev(p1 :Point,p2 :Point):Number
		{
			var absX :Number = p2.x - p1.x;
			var absY :Number = p2.y - p1.y;
			var tanVal :Number = Math.abs(absY / absX);
			var degrees :Number = Math.atan(tanVal) * 180 / Math.PI;
			
			if(degrees == 0 && absX < 0 )//x 轴反
			{
				degrees = 180;
			}
			if(degrees == 90 && absY > 0)//y 轴反
			{
				degrees = 270;
			}
			if(absX > 0 && absY < 0)//第一象限
			{
				
			}
			if(absX < 0 && absY < 0)//第二象限
			{
				degrees = 180 - degrees;
			}
			if(absX < 0 && absY > 0)//第三象限
			{
				degrees = 180 + degrees;
			}
			if(absX > 0 && absY > 0)//第四象限
			{
				degrees = 360 - degrees;
			}
			return degrees;
		}
	}
}